<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="visible" width="600px" v-if="visible" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :disabled="disabled">
        <el-form-item label="发行方名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入发行方名称" maxlength="50" :style="{width: inputWidth}"></el-input>
        </el-form-item>
        <el-form-item label="上传logo" prop="logo" ref="imgRef">
          <ImageUploadMore :limit="1" :fileSize="20" @successImg="getImg" :value="form.logo">
          </ImageUploadMore>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm" v-if="!disabled">保存提交</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
        <el-button @click="cancel" v-if="disabled">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addIssuer,
    getIssuer,
    updateIssuer,
  } from "@/api/equityManager/IssuerConfigure";

  export default {
    // dicts: ['asset_card_type'],
    name: "Dialog",
    data() {
      return {
        labelWidth: '120px',
        inputWidth: '300px',
        id: '', //
        submitLoading: false,
        // 是否禁用表单
        disabled: false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        visible: false,
        //数组
        cardOptin: [],
        // 表单参数
        form: {
          id:'',//发行方id
          name: '', //发行方名称
          logo: '', //logo
        },
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "请输入发行方名称",
            trigger: ["blur", "change"]
          }],
          logo: [{
            required: true,
            message: '请上传logo',
            trigger: ['blur', 'change']
          }, ],
        },
      };
    },
    created() {

    },
    methods: {
      /** 打开发行管理弹框 */
      open(id, disabled) {
        this.reset();
        this.disabled = disabled
        this.form.id = id
        if (id) {
          getIssuer(id).then(response => {
            this.form = response.result;
          });
        }
        this.visible = true
        if (this.disabled) {
          this.title = "新增发行方"
        } else if (id) {
          this.title = "编辑发行方"
        } else {
          this.title = "新增发行方"
        }
      },
      // 取消按钮
      cancel() {
        this.visible = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id:'',
          name: '', //发行方名称
          logo: '', //logo
        }
      },
      /** logo */
      getImg(e) {
        this.form.logo = e
        this.$refs['imgRef'].clearValidate();
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.submitLoading = true
            if (this.form.id != null) {
              updateIssuer(this.form).then(response => {
                this.$message.success('修改成功');
                this.cancel();
                this.$emit('refresh')
                this.submitLoading = false
              }).catch(() => {
                this.submitLoading = false
              });
            } else {
              addIssuer(this.form).then(response => {
                this.$message.success('新增成功');
                this.cancel();
                this.$emit('refresh')
                this.submitLoading = false
              }).catch(() => {
                this.submitLoading = false
              });
            }
          }
        });
      },
      /** 序号 */
      rowIndex({
        row,
        rowIndex
      }) {
        row.index = rowIndex + 1;
      },
    }
  };

</script>
<style scoped>
  .img_box {
    width: 100px;
    height: 70px;
    border-radius: 4px;
  }

</style>
